<?php

namespace Controller;

//Inkluderar controllrar
require_once("Controller/User_Controller.php");
require_once("Controller/Login_Controller.php");
require_once("Controller/Topic_Controller.php");
require_once("Controller/Post_List_Controller.php");
require_once("Controller/New_Topic_Controller.php");
require_once("Controller/Topic_List_Controller.php");
require_once("Controller/Static_Content_Controller.php");
require_once("Controller/My_Topics_Posts_Controller.php");
require_once("Controller/Custom_Topic_List_Controller.php");

//Inkluderar vyer
require_once("View/Page_View.php");
require_once("View/Post_View.php");
require_once("View/User_View.php");
require_once("View/Login_View.php");
require_once("View/Topic_View.php");
require_once("View/Navigation_View.php");
require_once("View/Static_Content_View.php");
require_once("View/Page_Composition_View.php");

//Inkluderar modellklasser
require_once("Model/Post_Model.php");
require_once("Model/Topic_Model.php");
require_once("Model/Post_List_Model.php");
require_once("Model/Topic_List_Model.php");
require_once("Model/Database/Database.php");
require_once("Model/Database/DBConfig.php");
require_once("Model/Database/DBTables.php");
require_once("Model/User_Handler_Model.php");
require_once("Model/Login_Handler_Model.php");

class MasterController 
{
		//Retunerar HTML sträng via PageView
        public static function DoControll() 
        {
        	//Öppnar databas
        	$db = new \Model\Database();
        	$db->Connect(new \Model\DBConfig());
								
        	//Skapar vy, kontroll och modell-instanser som alltid kommer köras
			$navView = new \View\NavigationView();
			$pageComp = new \View\PageCompositionView();
			$pageView = new \View\PageView();
			$topicView = new \View\TopicView();
			
			$loginCtrl = new \Controller\LoginController();
			$customTopicListCtrl = new \Controller\CustomTopicListController();	 
			$staticContentCtrl = new \Controller\StaticContentController();

			$topicListMdl = new \Model\TopicList($db);
			$loginMdl = new \Model\LoginHandler($db);
						
			//Kör de kontrollrar som alltid skall köras
			$login = $loginCtrl->DoControll($db, $loginMdl);//Retunerar två HTML strängar; Loginpanel & Userpanel				
			$mostPopularTopics = $customTopicListCtrl->DoControll($topicListMdl, $topicView); //Retunerar topiclista för sidebar	
			$staticHTML = $staticContentCtrl->DoControll();					
			
			//Resterande kontrollrar körs beroende på användarinteraktion
			if(\View\NavigationView::IsViewingRegisterForm() == TRUE)
			{
				$userCtrl = new \Controller\UserController();
				$html = $userCtrl->DoControll($db, $loginMdl); 
			}
			else if(\View\NavigationView::isViewingTopic() == TRUE)
			{		
				$topicCtrl = new \Controller\TopicController();	
				$ctrlOutput = $topicCtrl->DoControll($db, $topicListMdl, $topicView, $loginMdl);		
				$html = $pageComp->MergeTopicAndPost($ctrlOutput->topicHTML, $ctrlOutput->postHTML);
			}
			else if(\View\NavigationView::IsViewingNewTopicForm() == TRUE)
			{
				$newTopicCtrl = new \Controller\NewTopicController();
				$html = $newTopicCtrl->DoControll($topicListMdl, $topicView, $loginMdl); 				
			}
			else if(\View\NavigationView::IsViewingMyTopicsAndPosts() == TRUE)
			{
				$myTopicsAndPosts = new \Controller\MyTopicsAndPostsController();				
				$ctrlOutput = $myTopicsAndPosts->DoControll($db, $topicListMdl, $loginMdl, $topicView);	
				$html = $pageComp->MergeTopicAndPost($ctrlOutput->topicHTML, $ctrlOutput->postHTML);							
			}
			else if(\View\NavigationView::IsViewingAboutPage() == TRUE)
			{
				$html = $staticHTML->aboutHTML;						
			}										
			else
			{
				$topicListCtrl = new \Controller\TopicListController();				
				$html = $topicListCtrl->DoControll($topicListMdl, $topicView);
			}
			
			//Fördelar uttdata						
			$loginPanel = $login->loginPanelHTML;
			$content = $html;
			$subContent = $pageComp->MergeSubContent($login->userPanelHTML, $mostPopularTopics);
			$footer = $staticHTML->footerHTML;
						
			//Stänger databas  
			$db->Close();
			
			//Retunerar sidinnehåll via PageView                                 
            return $pageView->GetHTMLPage("TrashTalk.com", $loginPanel, $content, $subContent, $footer);
        }
        
}

echo MasterController::doControll();

?>
